#ifndef __ITKEXT__IMAGE__IMAGEMOMENTS__TXX__
#define __ITKEXT__IMAGE__IMAGEMOMENTS__TXX__

namespace itkExt
{
  // -----------------------------------------------------------------------
  template< class I, class S, class M >
  void ImageMoments< I, S, M >::
  Compute( const TRegion& reg )
  {
    this->m_Calculator.Reset( );

    _TIterator iIt( this->m_Image, reg );
    for( iIt.GoToBegin( ); !iIt.IsAtEnd( ); ++iIt )
    {
      TPixel val;
      TVector vec;
      if( this->_ComputeValue( val, vec, iIt ) )
        this->m_Calculator.AddMass( vec, TScalar( val ) );

    } // rof
    this->m_Calculator.Compute( );
  }

  // -----------------------------------------------------------------------
  template< class I, class S, class M >
  ImageMoments< I, S, M >::
  ImageMoments( )
    : Superclass( ),
      m_Image( NULL ),
      m_ImageMask( NULL ),
      m_SpatialMask( NULL ),
      m_PixelFunction( NULL )
  {
  }

  // -----------------------------------------------------------------------
  template< class I, class S, class M >
  ImageMoments< I, S, M >::
  ~ImageMoments( )
  {
  }

  // -----------------------------------------------------------------------
  template< class I, class S, class M >
  bool ImageMoments< I, S, M >::
  _ComputeValue( TPixel& v, TVector& p, const _TIterator& i ) const
  {
    // Evaluated point
    TPoint pnt;
    this->m_Image->TransformIndexToPhysicalPoint( i.GetIndex( ), pnt );

    // Return values
    bool ret = true;
    v = i.Value( );
    p = pnt.GetVectorFromOrigin( );

    // Check geometrical mask
    if( this->m_SpatialMask.IsNotNull( ) )
      ret = this->m_SpatialMask->IsInside( pnt );

    // Check image mask
    if( ret && this->m_ImageMask.IsNotNull( ) )
    {
      TIndex mIdx;
      if( this->m_ImageMask->TransformPhysicalPointToIndex( pnt, mIdx ) )
        v *= TPixel( this->m_ImageMask->GetPixel( mIdx ) );

    } // fi

    // Compute pixelwise function
    if( ret && this->m_PixelFunction.IsNotNull( ) )
      v = this->m_PixelFunction->Evaluate( v );

    return( ret );
  }

} // ecapseman

#endif // __ITKEXT__IMAGE__IMAGEMOMENTS__TXX__

// eof - $RCSfile: itkExtImageMoments.txx,v $
